﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data.Items;
using System.Data;
using Workflow.Alerts.DAL;
using System.Text;
using System.Globalization;
using workflow.Dal;

namespace Workflow.Alerts
{
    public class PartApproveViewPresenter : ActionPresenter
    {
        #region members
        private readonly IPartApproveView view;
        #endregion

        #region constructor
        public PartApproveViewPresenter(IPartApproveView _view)
            : this(_view, null)
        { }

        public PartApproveViewPresenter(IPartApproveView _view, ISessionProvider session)
            : base(_view, session)
        {
            view = base.GetView<IPartApproveView>();
            view.PartApproveLoad += view_OnLoad;
            view.PartApproveSave += view_OnSave;
        }
        #endregion

        #region private methods
        private void view_OnLoad()
        {
            using (WorkflowEntitySet context = new WorkflowEntitySet())
            {
                var tempChanges = TempChanges;

                var alert = tempChanges.Where(a => a.id == view.AlertId).FirstOrDefault();
                if (alert == null)
                    alert = context.Alert.Where(a => a.id == view.AlertId).First();

                view.StartDate = alert.estimatestartdate;
                view.EndDate = alert.estimateenddate;                
                var comments = alert.Comments1.OrderBy(c => c.createddatetime).FirstOrDefault();
                if (comments != null)
                {
                    view.Comments = comments.comment;
                }
            }


            //var changedAlert = Changes.FindByid(view.AlertId);
            //if (changedAlert != null && (changedAlert.newstatus == (int)WorkflowEnums.Status.PartiallyApproved))
            //{
            //    view.Comments = changedAlert.comments;
            //    if (!String.IsNullOrEmpty(changedAlert.startdate))
            //        view.StartDate = DateTime.Parse(changedAlert.startdate);
            //    if (!String.IsNullOrEmpty(changedAlert.enddate))
            //        view.EndDate = DateTime.Parse(changedAlert.enddate);
            //    if (String.IsNullOrEmpty(changedAlert.trafficproblems))
            //        view.TrafficProblems = false;
            //    else
            //        view.TrafficProblems = Boolean.Parse(changedAlert.trafficproblems);
            //}
            //else
            //{
            //    AlertViewDAL dal = new AlertViewDAL();
            //    var filter = new WorkflowFilter() { AlertId = view.AlertId };
            //    EstateDataSet.AlertDetailsDataTable alerts = dal.GetAlertDetails(new WorkflowFilterSql(filter));
            //    if (alerts != null && alerts.Rows.Count == 1)
            //    {
            //        EstateDataSet.AlertDetailsRow alert = alerts[0];

            //        view.StartDate = alert.estimatestartdate;
            //        view.EndDate = alert.estimateenddate;
            //        view.TrafficProblems = alert.trafficproblems;
            //    }
            //    else
            //    {
            //        view.TrafficProblems = false;
            //    }
            //}
        }

        private void view_OnSave()
        {

            using (WorkflowEntitySet context = new WorkflowEntitySet())
            {
                var tempChanges = TempChanges;

                var alert = tempChanges.Where(a => a.id == view.AlertId).FirstOrDefault();
                if (alert != null)
                    tempChanges.Remove(alert);

                alert = context.Alert.Where(a => a.id == view.AlertId).First();
                alert.estimateenddate = view.EndDate.Value;
                alert.estimatestartdate = view.StartDate.Value;                
                alert.currentstatus = (int)WorkflowEnums.Status.PartiallyApproved;

                var comments = new Comments()
                {
                    Person = context.Person.Where(p => p.id == CurrentUser.Id).First(),
                    createddatetime = DateTime.Now,
                    comment = view.Comments
                };
                alert.Comments1.Add(comments);
                tempChanges.Add(alert);

                TempChanges = tempChanges;
            }



            //WorkflowEnums.Status newStatus = WorkflowEnums.Status.PartiallyApproved;
            //WorkflowEnums.Status oldStatus = WorkflowEnums.Status.Sent;

            //UpdateStatus(view.AlertId, newStatus, oldStatus, view.Comments, view.StartDate, view.EndDate, view.TrafficProblems);
        }

        #endregion
    }
}
